package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车信息:{}", shoppingCart);
        //设置用户id,指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //查询当前菜品和套餐信息
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //查询用户
        lqw.eq(ShoppingCart::getUserId, currentId);
        if (dishId != null) {
            //添加到购物车的是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
            //查询菜品表找到当前套餐中要添加的id
        } else {
            //添加的是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询当前菜品或者套餐是否在购物车中
        //SQl:select * from shopping_Cart where user_id = ? add dish_id/setmeal_id=?
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);

        if (cartServiceOne != null) {
            //已存在+1
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //不存在添加
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        //根据当前登录的用户id从购物车表里查询对应的购物车信息
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //添加排序条件
        lqw.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getId, BaseContext.getCurrentId());
        shoppingCartService.remove(lqw);
        return R.success("清除成功");
    }

    @PostMapping("/sub")//一开始用订单数据接收的方法不行,因为只能获取套餐或者菜品的,做不到都删除的效果(从else里面取出来再用个if就行了!!!谨记!!!)
    //借鉴宝钢的用map来接收数据,就可以多次操作   !!!验证用户!!!
    public R<ShoppingCart> sub(@RequestBody Map map) {
        //获取当前登录用户的id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //判断当前操作的是菜品还是套餐
        lqw.eq(ShoppingCart::getUserId,userId);
        lqw.eq(map.get("dishId") != null, ShoppingCart::getDishId, map.get("dishId"));
        lqw.eq(map.get("setmealId") != null, ShoppingCart::getSetmealId, map.get("setmealId"));
        //判断菜品口味信息
        lqw.eq(map.get("dishFlavor") != null, ShoppingCart::getDishFlavor, map.get("dishFlavor"));
        ShoppingCart one = shoppingCartService.getOne(lqw);
        if (one != null) {
            if (one.getNumber() > 1) {
                one.setNumber(one.getNumber()-1);
                shoppingCartService.updateById(one);
            }else if (one.getNumber()==1){
                shoppingCartService.removeById(one);
                ShoppingCart temp = new ShoppingCart();
                return R.success(temp);
            }
        } else {
            return R.error("当前网络异常,请检查网络");
        }
        return R.success(one);
    }

}
